/*
 * Uncompleted object picker
 * 
 */
using System;
using IrrlichtNETCP;

namespace kinematix
{
	
	
	public class kObjectPicker
	{
		
		IrrlichtDevice device;
		SceneManager scene;
		Position2D p;
		SceneNode picked;		
		SceneNode lastPicked;
		SceneNode lastSelected;		
		
		public kObjectPicker(IrrlichtDevice _device)
		{
			device = _device;
			scene = device.SceneManager; 
		}
		
		
		/*
		 * On mouse move handler
		 */
		public void OnMouseMove(Position2D pos)
		{
			p = pos;
			// take the scene node under cursor
			picked = scene.CollisionManager.GetSceneNodeFromScreenCoordinates(p, 0, false);
			if (picked != null) //picked one
			{
				if (picked.ID != -1) // not environment but physics object 
				{
					if (lastPicked != null) // if there wasn't picked earlier one
					{
						if (picked != lastPicked) // if was check, is it the same
						{
							//deselect the parent of the picked node, because the 
							(lastPicked.Parent as kPhysNode).Deselect();
						}
			
					} // otherwise select
							(picked.Parent as kPhysNode).Select();				
							lastPicked = picked; //store the current picked object					
				}
				
			}	
			
			
		}
		
		/*
		 * On click handler
		 */
		public void OnClick(Position2D pos)
		{
			p = pos;
			// take the node
			picked = scene.CollisionManager.GetSceneNodeFromScreenCoordinates(p, 0, false);		
			if (picked != null)
			{
				if (picked.ID != -1)
				{
					// get the parent of the picked node, because 
						kPhysNode pn = picked.Parent as kPhysNode;
					// this must be clickable
						if (pn.Selectable)
						{
							if (!pn.Selected) // if it wasn't selected before select it
							{	
								if (lastSelected != picked)
									{
										if (lastPicked != null){
										(lastSelected.Parent as kPhysNode).Deselect();}
										pn.Select();
										lastPicked = picked;
									} else {
										pn.Deselect();
										lastSelected = null;
									}
							} else {
								pn.Deselect(); 
								lastSelected = null;
							}
						}
						// fire a click event to the scene node
						pn.Click();
						
				}
			}
		}
		
	}
}
